/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Module;
import java.sql.*;
import java.util.Vector;

/**
 *
 * @author mishra
 */
public class Query {
    static Connection con=null;
    
    public static int add(ModuleBean mb) throws SQLException
    {
        int i=0,k;
        try
        {
            con=DBConnection.Connection.getConnection();
            PreparedStatement pre=con.prepareStatement("insert into module values(moduleid.nextval,?,sysdate)");
            pre.setString(1, mb.getModuleName());
            k=pre.executeUpdate();
            if(k!=0)
            {
                i=getMaxId();
            }
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
        finally
        {
            con.close();
        }
        return i;
    }
    public static int getMaxId() throws SQLException
    {
        int i=0;
        try
        {
            con=DBConnection.Connection.getConnection();
            PreparedStatement pre=con.prepareStatement("select max(module_id) from module");
            ResultSet rs=pre.executeQuery();
            while(rs.next())
            {
                i=rs.getInt(1);
            }
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
        return i;
    }
public static ModuleBean viewSpecific(int val) throws SQLException
{
    ModuleBean mb= new ModuleBean();
    try{
        con=DBConnection.Connection.getConnection();
        PreparedStatement pre=con.prepareStatement("select * from module where MODULE_ID=?");
        pre.setInt(1, val);
        ResultSet rs=pre.executeQuery();
        while(rs.next())
        {
            mb.setModuleId(rs.getInt(1));
            mb.setModuleName(rs.getString(2));
            mb.setEntryDate(rs.getDate(3));
        }
        
    }catch(Exception e)
    {
        System.out.println(e);
    }finally{
        con.close();
    }
    return mb;
}
    
public static Vector viewAll() throws SQLException
{
    Vector<Object> list=new Vector<Object>();
    
    try{
        con=DBConnection.Connection.getConnection();
        PreparedStatement pre=con.prepareStatement("select * from module order by module_id");
        ResultSet rs=pre.executeQuery();
        while(rs.next())
        {
            ModuleBean mb= new ModuleBean();
            mb.setModuleId(rs.getInt(1));
            mb.setModuleName(rs.getString(2));
            mb.setEntryDate(rs.getDate(3));
            list.add(mb);
        }
        
    }catch(Exception e)
    {
        System.out.println(e);
    }finally{
        con.close();
    }
    return list;
}

public static int edit(ModuleBean mb) throws SQLException {
        int i = 0;
        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("update module set module_name=?, entrydate=sysdate where module_id=?");
            pre.setString( 1, mb.getModuleName());
            pre.setInt(2, mb.getModuleId());
            i = pre.executeUpdate();

        } catch (Exception e) {
            System.out.println(e);
        } finally {
            con.close();
        }
        return i;
    }
    public static int delete(int id) throws SQLException
        {
            int i=0;
            try
            {
              con=DBConnection.Connection.getConnection();
              PreparedStatement pre=con.prepareStatement("delete from module where module_id=?");
              pre.setInt(1,id);
              i=pre.executeUpdate();
            }
            catch(Exception e)
            {
               return i; 
            }
            finally
            {
              con.close();  
            }
            return i;
        }   
}
